package getKData;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GETFUT1MKDATA {
    private static final String benter="\n";
    private static final String cm=",";
    private static ArrayList<String> al = new ArrayList<String>();
    private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    private static final DateFormat dh = new SimpleDateFormat("HH:mm:ss");
    private static final DecimalFormat ni=new DecimalFormat("######.00");
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			getHttp("http://192.168.5.177:8000/FITX_1MK","");
			genData();
		}catch(Exception e){
			System.out.println("error:"+e);
		}finally{
			System.out.println("執行完成!");
		}
	}
    private static void genData(){
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        DateFormat dateFormat1 = new SimpleDateFormat("HHmmss");
        FileWriter fw=null;
        try {
            fw = new FileWriter("c:\\aa.txt");
            fw.write("\"Date\",\"Time\",\"Open\",\"High\",\"Low\",\"Close\",\"TotalVolume\""+benter);
            for(int i=al.size()-1;i>0;i--){
                String a=al.get(i);
                if(a.length()<20){
                    continue;
                }
//                System.out.println(a);
                String day,hour,o,h,l,c,volume;
                String b[] = a.split("[ ]");

                Date today = dateFormat.parse(b[1]);
                Date hour1 = dateFormat1.parse(b[2]);
    //                ni.format(b[3]);//開,高,低,收,
                fw.write(df.format(today)+cm+b[2]+cm+
                        ni.format(Integer.parseInt(b[3]))+cm+ni.format(Integer.parseInt(b[4]))+cm+
                        ni.format(Integer.parseInt(b[5]))+cm+ni.format(Integer.parseInt(b[6]))+cm+
                        b[7]+benter);
            }
        } catch (Exception ex) {
        }finally{
            if(fw!=null){
                try{
                    fw.close();
                }
                catch(IOException ioe){} 
            }
        }
    }
    private static void getHttp(String ipAddr,String ca) throws IOException {
        URL u;
        String thisLine="";
        u = new URL(ipAddr);
        //BufferedReader theHTML=new BufferedReader(new InputStreamReader(u.openStream()));
        HttpURLConnection myConn=(HttpURLConnection)u.openConnection();
        myConn.setDoOutput(true);
        myConn.setRequestMethod("POST");
        //myConn.setRequestMethod(HttpConnection.POST);
        myConn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );//
        //OutputStreamWriter myOut=new OutputStreamWriter(myConn.getOutputStream(),"Big5");
        OutputStreamWriter myOut=new OutputStreamWriter(myConn.getOutputStream());
        //System.out.println("kind=D&account="+sData);
        myOut.write(ca);
        myOut.flush();
        myOut.close();
        BufferedReader br = new BufferedReader(new InputStreamReader(myConn.getInputStream(),"Big5"));
        while((thisLine=br.readLine())!=null){
            al.add(thisLine);
        }
        br.close();
        myConn = null;
    }
}
